#encoding=utf-8
import datetime
import sys
import time
import os
import random
from multiprocessing import Pool
'''
对impala进行分区创建
'''
create = "ALTER TABLE blackpanther.tbl_man_warning_rt ADD IF NOT EXISTS PARTITION (startdate_day='{0}',startdate_min='{1}');"
start_pool = 8
hosts = ['dn32', 'dn33', 'dn34', 'dn35', 'dn36', 'dn37', 'dn38', 'dn39', 'dn40', 'dn41', 'dn42', 'dn43']

def handle_start_end_time():
    '''
    处理开始时间和结束时间之间的时间，时间间隔为分钟
    :return:
    '''
    start_time = str(sys.argv[1])
    start_time = datetime.datetime.strptime(start_time, '%Y%m%d')
    # 获取结束的时间
    end_time = str(sys.argv[1])
    end_time = datetime.datetime.strptime(end_time, '%Y%m%d')
    end_time = end_time + datetime.timedelta(days=1,minutes=-1)
    # 存放过程中的时间
    middle_times = [start_time.strftime('%Y%m%d%H%M')]
    while True:
        start_time = start_time + datetime.timedelta(minutes=1)
        if start_time.day <= end_time.day:
            middle_times.append(start_time.strftime('%Y%m%d%H%M'))
        else:
            break
    return middle_times


def init_impala_command():
    commands = []
    middle_times = handle_start_end_time()
    shells = []
    for mt in middle_times:
        tsp = create.format(mt[:-4], mt)
        shells.append(tsp)
        if len(shells) >= 10:
            commands.append('impala-shell -i {0} -q "{1}"'.format(random.choice(hosts), ''.join(shells)))
            shells.clear()
    print 'execute impala create partition start_time = {0}, end_time = {1}'.format(commands[0], commands[-1])
    return commands


def execute_impala(impala_command_list):
    for hcl in impala_command_list:
        start_time = time.time()
        # result = os.popen(hcl).read()
        print(hcl)
        end_time = time.time()
        #print('command={0},result={1},use_time={2}\n'.format(hcl, result, end_time - start_time))


def execute_pool():
    '''
    执行进程的方法
    @:param commands 所有的 hadoop命令
    @:param start_pool 需要开启的进程的数量
    '''
    commands = init_impala_command()
    pool = Pool(start_pool)
    start_pool_list = [commands[i:i + start_pool] for i in range(0, len(commands), start_pool)]
    for spl in start_pool_list:
        pool.apply_async(execute_impala, (spl,))
    pool.close()
    pool.join()


if __name__ == '__main__':
    start_time = time.time()
    execute_pool()
    end_time = time.time()
    print(end_time - start_time)
